<?php
namespace app\admin\controller;

use app\common\model\AfficheModel;
use app\common\model\AfficheTypeModel;
use think\Lang;


/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/10/10
 * Time: 21:05
 */
class Affiche extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/affiche.lang.php');

        
    }

    /**
     * @description:   @MrLiu
     * @return mixed
     */
    public function index()
    {
        $affiche = model('afficheModel');
        $condition = array();
        $title = input('param.title', '');
        $type_id = input('param.type_id', '');
        if (!empty($title)) {
            $condition['af.title'] = $title;
        }
        if (!empty($type_id)) {
            $condition['af.typeid'] = intval($type_id);
        }
        $list = $affiche->getAfficheList($condition, 7);
        $afficheType = model('afficheTypeModel');
        $typeList = $afficheType->getAfficheTypeList();
        $this->assign('list', $list);
        $this->assign('type_list', $typeList);
        $this->assign('title', $title);
        $this->assign('type_id', $type_id);
        $this->assign('show_page', $affiche->page_info->render());

        $this->setAdminCurItem('index');
       return $this->fetch();
    }

    /**
     * @description:   @MrLiu
     * @param AfficheTypeModel $afficheType
     * @param AfficheModel $affiche
     * @return mixed
     */
    public function edit(AfficheTypeModel $afficheType,  AfficheModel $affiche)
    {
        $data = array();
        $affiche_id = intval(input('param.affiche_id'));

        if (!empty($affiche_id) && $affiche_id > 0) {
            
            $data = $affiche->getOneAfficheById($affiche_id);
        }else{
            $data['typeid'] = 0;
        }

        $typeList = $afficheType->getAfficheTypeList();

        $this->assign('data', $data);
        $this->assign('affiche_id', $affiche_id);
        $this->assign('typeList', $typeList);

        $this->setAdminCurItem('add');
        return $this->fetch('edit');
    }

    /**
     * @description:   @MrLiu
     * @param AfficheModel $afficheModel
     */
    public function save(AfficheModel $afficheModel)
    {
        $id = intval(input('param.id'));
        $data = array(
            'title' => input('param.title'),
            'typeid' => input('param.typeid'),
            'start_time' => strtotime(input('param.start_at')),
            'end_time' => strtotime(input('param.end_at')),
            'opration_id' => $this->admin_info['admin_id'],
            'sort' => intval(input('param.sort', 0)),
            'contents' => input('param.contents'),
        );

        //推送
        // $result = SendAll($data['contents']);

        $validate = validate('affiche');
        if (!$validate->scene('affiche')->check($data)) {
            $this->error($validate->getError());
        }

        if (!$this->check_time($data['start_time'], $data['end_time'])) {
            $this->error(lang('affiche_time_error'));
        }

        #统计各个人数
        switch ($data['typeid']) {
            case 1:
                #查所有的服务人员
                $where['status'] = 1;
                $count_num = db('fuwu_personal')->where($where)->count();
                $data['unread'] = $count_num;
                pushMessageToApp($data['title'],$data['contents']);
                break;
            case 2:
                #查所有的客户端人员
                $count_num = db('member')->count();
                $data['unread'] = $count_num;
                break;
            case 3:
                #查所有的运营商人员
                $where1['store_seller_type'] = 1;
                $count_num = db('store')->where($where1)->count();
                $data['unread'] = $count_num;
                break;
            case 4:
                #查所有的供应商人员
                $where2['store_seller_type'] = 2;
                $count_num = db('store')->where($where2)->count();
                $data['unread'] = $count_num;
                break;
            case 5:
                #查所有的客服人员
                $where3['member_type'] = 10;
                $count_num = db('member')->where($where3)->count();
                $data['unread'] = $count_num;
                break;                 
            default:
                break;
        }

        if ($id) {
            $status = $afficheModel->editAffiche($data, $id);
        } else {
            $status =$afficheModel->addAffiche($data);
        }

        if ($status>=0) {
            $this->log(lang('ds_edit') . lang('affiche_type_add') . '[' . $data['title'] . ']', 1);
            dsLayerOpenSuccess('保存成功');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * @description:   @MrLiu
     * @param AfficheTypeModel $afficheModel
     */
    public function cancels(AfficheModel $afficheModel)
    {
        $affiche_id = intval(input('param.affiche_id'));
        $status = $afficheModel->delAffiche($affiche_id);
        if ($status === false) {
            ds_json_encode(10001, lang('affiche_del_exists'));
        }
        $this->log(lang('ds_add') . lang('affiche_type_index_class') . '[ID:' . $affiche_id . ']', 1);
        ds_json_encode(10000, lang('affiche_del_success'));
    }

    //推送
    // addtag是设备别名，（具体给那个用户推荐，这个需要app端先去跟极光后台报备下，否则推送不成功）
    // content是我们推送时状态栏写的内容
    // id  和type是针对安卓版本的参数，如推送时声音可以自定义不一定用系统的声音（'title' => $content,）
    // public function addPush($addtag,$content,$id,$type){
    //     // $client = new Jpush(config('Jpush.APP_KEY'),config('Jpush.APP_DevSecret'));

    //     // $response = $client->push();
    //     // //设置发送的平台
    //     //    ->setPlatform(arrat('ios','android'))
    //     // // 别名 ，用于推送到指定用户
    //     //    ->addalias($addtag)
    //     // // 设置给ios推送
    //     //    ->iosNotifcation($content,array(
    //     //         'sound' => $type,
    //     //         'content-available' => true,
    //     //         'mutable-contnet' => true,
 
    //     //    ))
    //     //    ->option(array(
    //     // //设置推送环境是开发环境 或者上线模式
    //     //         'apns_production' => false,
    //     //    ))
    //     // // 推送   
    //     //    ->send();
    // }

    /**
     * @description: 时间戳比较  @MrLiu
     * @param $startTime
     * @param $endTime
     * @return bool
     */
    protected function check_time($startTime, $endTime)
    {
        if ($startTime < 0 || $endTime < 0) {
            return false;
        }
        if ($endTime > $startTime) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @description:   @MrLiu
     * @return array
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_manage'),
                'url' => url('Affiche/index')
            ),
        );

        if (request()->action() == 'edit' || request()->action() == 'index') {
            $menu_array[] = array(
                'name' => 'add',
                'text' => input('param.affiche_id') > 0  ? lang('ds_edit') : lang('ds_new'),
                'url' => url('Affiche/edit')
            );
        }

        return $menu_array;
    }
}